package com.quectel.core.module.propertybill.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.propertybill.dao.PropertyChargeableListDueDateDao;
import com.quectel.core.module.propertybill.dto.PropertyChargeableListDto;
import com.quectel.core.module.propertybill.dto.PropertyChargeableListDueDateDto;
import com.quectel.core.module.propertybill.entity.PropertyChargeableListDueDateEntity;
import com.quectel.core.module.propertybill.service.PropertyChargeableListDueDateService;
import com.quectel.core.module.propertybill.service.PropertyChargeableListService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/04/03 11:03
 */
@DubboService
public class PropertyChargeableListDueDateServiceImpl extends ServiceImpl<PropertyChargeableListDueDateDao, PropertyChargeableListDueDateEntity> implements PropertyChargeableListDueDateService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyChargeableListDueDateService.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PROPERTY_CHARGEABLE_LIST_DUE_DATE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PROPERTY_CHARGEABLE_LIST_DUE_DATE_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PropertyChargeableListDueDateDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PROPERTY_CHARGEABLE_LIST_DUE_DATE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PropertyChargeableListDueDateDao propertyChargeableListDueDateDao;

    @Autowired
    private PropertyChargeableListService propertyChargeableListService;


    @Override
    public PropertyChargeableListDueDateDto selectByRoomInfoAndListId(Long tenantId, Long villageId, Long roomId, Long chargeableListId) {
        PropertyChargeableListDueDateEntity entity = propertyChargeableListDueDateDao.selectOne(
                new LambdaQueryWrapper<PropertyChargeableListDueDateEntity>()
                        .eq(PropertyChargeableListDueDateEntity::getTenantId, tenantId)
                        .eq(PropertyChargeableListDueDateEntity::getVillageId, villageId)
                        .eq(PropertyChargeableListDueDateEntity::getRoomId, roomId)
                        .eq(PropertyChargeableListDueDateEntity::getChargeableListId, chargeableListId)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return paddingField(CopyUtils.copyObj(entity, PropertyChargeableListDueDateDto.class));
    }

    @Override
    public void saveBatch(List<PropertyChargeableListDueDateDto> propertyChargeableListDueDateDtos) {
        List<PropertyChargeableListDueDateEntity> entityList = CopyUtils.copyList(propertyChargeableListDueDateDtos, PropertyChargeableListDueDateEntity.class);
        entityList.forEach(v -> {
            v.setId(Snowflake.nextId());
        });
        super.saveBatch(entityList);
    }

    @Override
    public PropertyChargeableListDueDateDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PROPERTY_CHARGEABLE_LIST_DUE_DATE_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PropertyChargeableListDueDateDto selectById(Long id) {
        PropertyChargeableListDueDateEntity entity = propertyChargeableListDueDateDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, PropertyChargeableListDueDateDto.class));
    }

    @Override
    public List<PropertyChargeableListDueDateDto> queryList(Map<String, Object> params) {
        List<PropertyChargeableListDueDateEntity> list = propertyChargeableListDueDateDao.queryList(params);
        List<PropertyChargeableListDueDateDto> result = CopyUtils.copyList(list, PropertyChargeableListDueDateDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PropertyChargeableListDueDateDto paddingField(PropertyChargeableListDueDateDto dto) {
        if (dto != null) {
            PropertyChargeableListDto propertyChargeableListDto = propertyChargeableListService.selectCacheById(dto.getChargeableListId());
            if (propertyChargeableListDto != null) {
                dto.setChargeableListName(propertyChargeableListDto.getName());
            }
            dto.setChargeableListDto(propertyChargeableListDto);

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return propertyChargeableListDueDateDao.queryTotal(params);
    }

    @Override
    public Long save(PropertyChargeableListDueDateDto dto) {
        PropertyChargeableListDueDateEntity entity = CopyUtils.copyObj(dto, PropertyChargeableListDueDateEntity.class);
        entity.setId(Snowflake.nextId());
        propertyChargeableListDueDateDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PropertyChargeableListDueDateDto dto) {
        PropertyChargeableListDueDateEntity entity = CopyUtils.copyObj(dto, PropertyChargeableListDueDateEntity.class);

        propertyChargeableListDueDateDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PropertyChargeableListDueDateDto dto) {
        PropertyChargeableListDueDateEntity entity = CopyUtils.copyObj(dto, PropertyChargeableListDueDateEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, propertyChargeableListDueDateDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        propertyChargeableListDueDateDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
